package com.hopes.bizz.packet.hj212.handler;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hopes.bizz.packet.hj212.constant.HJ212Constant;
import com.hopes.bizz.packet.hj212.domain.monitor.constant.PollConstant;
import com.hopes.bizz.packet.hj212.domain.monitor.entity.PollMonitorFactorEntity;
import com.hopes.bizz.packet.hj212.domain.monitor.entity.PollMonitorSiteEntity;
import com.hopes.bizz.packet.hj212.domain.monitor.service.PollMonitorFactorService;
import com.hopes.bizz.packet.hj212.domain.monitor.service.PollMonitorSiteService;
import com.hopes.bizz.packet.hj212.support.Hj212DataAdapter;
import com.hopes.bizz.packet.hj212.support.Hj212DataStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lyb
 * @version 1.0
 * @description: TODO
 * @date 2023/2/27 21:45
 */
@Component
public class PollHj212DataSendHandler implements Hj212DataSendHandler {

    @Autowired
    private PollMonitorSiteService pollMonitorSiteService;

    @Autowired
    private PollMonitorFactorService pollMonitorFactorService;

    @Autowired
    private Hj212DataStore hj212DataStore;

    @Override
    public List<Hj212DataAdapter> handle(String cn) {
        List<Hj212DataAdapter> res = new ArrayList<>();
        Map<String, Map<String, Integer>> factorAlarmMap = hj212DataStore.getFactorAlarmMap();
        // 过滤指定mn号
        Set<String> mns = factorAlarmMap.keySet();
        Map<String, String> substringMap = hj212DataStore.getSubstringMap();

        LambdaQueryWrapper<PollMonitorSiteEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PollMonitorSiteEntity::getIsUse, true);
        List<PollMonitorSiteEntity> list = pollMonitorSiteService.list(wrapper).stream()
                .filter(it -> mns.contains(it.getCode())).collect(Collectors.toList());

        Date date = new Date();
        for (PollMonitorSiteEntity monitorSite : list) {
            Hj212DataAdapter adapter = new Hj212DataAdapter();
            if (monitorSite.getMonitorType().equals("21")) {
                adapter.setZsFlag(true);
            }
            String monitorType = monitorSite.getMonitorType();
            if (PollConstant.MONITOR_TYPE_GAS.equals(monitorType)) {
                adapter.setSt(HJ212Constant.POLLUTION_SOURCE_OF_ATMOSPHERIC_ENV);
            } else if (PollConstant.MONITOR_TYPE_WATER.equals(monitorType)) {
                adapter.setSt(HJ212Constant.POLLUTION_SOURCE_OF_SURFACE_WATER);
            }
            adapter.setCn(cn);
            adapter.setMn(monitorSite.getCode());
            adapter.setDataTime(date);

            PollMonitorFactorEntity param = new PollMonitorFactorEntity();
            param.setMonitorSiteId(monitorSite.getId());
            List<PollMonitorFactorEntity> factorList = pollMonitorFactorService.list(Wrappers.query(param));

            if (CollUtil.isNotEmpty(factorList)) {
                adapter.setDataList(factorList);
            }

            if (CollUtil.isNotEmpty(factorAlarmMap)) {
                adapter.setFactorAlarmMap(factorAlarmMap.get(monitorSite.getCode()));
            }

            if (CollUtil.isNotEmpty(substringMap)) {
                adapter.setSubstringMap(substringMap);
            }
            res.add(adapter);
        }
        return res;
    }
}
